package com.android.lovely.ui.dialog

import androidx.core.os.bundleOf
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.viewmodel.initializer
import androidx.lifecycle.viewmodel.viewModelFactory
import com.android.lovely.R
import com.android.lovely.databinding.DialogChatModelBinding
import com.android.lovely.databinding.DialogChatUserBinding
import com.android.lovely.framework.BaseDialogFragment
import com.android.lovely.ui.chat.ChatMessageActivity
import com.android.lovely.ui.chat.ConversationViewModel
import com.android.lovely.ui.chat.getChatId
import com.android.lovely.ui.viewmodel.ChatViewModel
import com.google.firebase.analytics.FirebaseAnalytics
import com.kotlin.extensions.appContext
import com.kotlin.extensions.view.ViewExt.clickable

/**
 *
 * @author why
 * @date 2025/3/30 15:03
 */
class ChatModelDialog: BaseDialogFragment<DialogChatModelBinding>() {
    private val viewModel by activityViewModels<ConversationViewModel> {
        viewModelFactory {
            initializer {
                ConversationViewModel(
                    getChatId()
                )
            }
        }
    }
    override fun initializeViews() {
        binding.ivClose.clickable {
            dismissAllowingStateLoss()
        }
        change(viewModel.chatMode)
        binding.clPassion.clickable {
            change(0)
            save(0)
        }
        binding.clTale.clickable {
            change(1)
            save(1)
        }
        binding.clDialogue.clickable {
            change(2)
            save(2)
        }
    }
    private fun change(mode: Int){
        binding.clPassion.isSelected = mode == 0
        binding.clTale.isSelected = mode == 1
        binding.clDialogue.isSelected = mode == 2
    }

    private fun save(mode: Int){
        viewModel.tempMode = mode
        dismissAllowingStateLoss()
        requireActivity().showLovelyDialog{
            setDialogTitle(getString(R.string.save_model))
            setPositiveButton(getString(R.string.delete_record)) {
                viewModel.clearConversation()
                val modeString = when (viewModel.tempMode) {
                    0 -> "passion"
                    1 -> "story"
                    2 -> "chat"
                    else -> ""
                }
                if (modeString.isNotEmpty()) {
                    FirebaseAnalytics.getInstance(appContext).logEvent(modeString, null)
                }
                viewModel.chatMode = viewModel.tempMode
                dismissAllowingStateLoss()
            }
            setNegativeButton(getString(R.string.keep_record)) {
                val modeString = when (viewModel.tempMode) {
                    0 -> "passion"
                    1 -> "story"
                    2 -> "chat"
                    else -> ""
                }
                if (modeString.isNotEmpty()) {
                    FirebaseAnalytics.getInstance(appContext).logEvent(modeString, null)
                }
                viewModel.chatMode = viewModel.tempMode
                dismissAllowingStateLoss()
            }
            setBottomClose(true)
        }
    }

    override fun isBottom(): Boolean {
        return true
    }

}